//
//  ViewController.swift
//  C150728_Login
//
//  Created by yao_yu on 15/7/28.
//  Copyright © 2015年 yao_yu. All rights reserved.
//

import UIKit

extension UIColor {
    
    convenience init(rgbString:String, alpha:CGFloat = 1.0) {
        let scanner = NSScanner(string: rgbString)
        var color:UInt32 = 0
        scanner.scanHexInt(&color)
        
        let mask = 0x000000FF
        let red = Int(color >> 16) & mask
        let green = Int(color >> 8) & mask
        let blue = Int(color) & mask
        
        self.init(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: alpha)
        
    }
}

extension UIImage {
    func transformToSize(newSize:CGSize) -> UIImage{
        UIGraphicsBeginImageContextWithOptions(newSize, false, UIScreen.mainScreen().scale)
        self.drawInRect( CGRectMake(0, 0, newSize.width, newSize.height))
        let transformedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return transformedImage
    }
}

extension UITextField {
    func setYYStyle() {
        self.backgroundColor = UIColor(rgbString: "F5FFFA", alpha: 0.5)
        self.layer.cornerRadius = 8.0
        self.layer.masksToBounds = true
        self.textAlignment = NSTextAlignment.Center
    }
}

extension UIButton {
    func setYYStyle() {
        self.backgroundColor = UIColor(rgbString: "ADD8E6", alpha: 0.9)
        self.layer.cornerRadius = 8.0
        self.layer.masksToBounds = true
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        let button = UIButton(type: UIButtonType.RoundedRect)
        button.frame = CGRectMake(20, 20, 200, 44)
        button.setTitle("测试", forState: UIControlState.Normal)
        view.addSubview(button)
        button.addTarget(self, action: "onLogin", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    override func viewDidAppear(animated: Bool) {
        onLogin()
    }
    
    func onLogin() {
        self.presentViewController(LoginViewController.instance, animated: true, completion: nil)
    }
}

class LoginViewController: UIViewController {
    
    static let instance = LoginViewController()
    
    let margin:CGFloat = 5
    let LABELWIDTH:CGFloat = 50
    let ITEMHEIGHT:CGFloat = 44
    let padding:CGFloat = 15
    var userTextField = UITextField()
    var passwordTextField = UITextField()
    var containView = UIView(frame: CGRectMake(0, 0, 300, 200))
    var backgroundView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        setupViews()
    }
    
    func setupViews() {
        let x = margin
        var y = margin
        let itemWidth = containView.bounds.width - x - margin
        userTextField.frame = CGRectMake(x, y, itemWidth, ITEMHEIGHT)
        userTextField.placeholder = "用户名"
        userTextField.setYYStyle()
        containView.addSubview(userTextField)
        y += ITEMHEIGHT + padding
        passwordTextField.frame = CGRectMake(x, y, itemWidth, ITEMHEIGHT)
        passwordTextField.placeholder = "密码"
        passwordTextField.setYYStyle()
        passwordTextField.secureTextEntry = true
        containView.addSubview(passwordTextField)
        view.addSubview(containView)
        
        backgroundView = UIImageView(image: UIImage(named: "3droad_splash.png")?.transformToSize(CGSizeMake(view.bounds.width, view.bounds.height)))
        backgroundView.center = view.center
        view.addSubview(backgroundView)
        view.sendSubviewToBack(backgroundView)
        
        let buttonLogin = UIButton(type: UIButtonType.System)
        buttonLogin.setTitle("登录", forState: UIControlState.Normal)
        y += ITEMHEIGHT + padding
        buttonLogin.frame = CGRectMake(x, y, itemWidth, ITEMHEIGHT)
        buttonLogin.setYYStyle()
        containView.addSubview(buttonLogin)
    }
    
    override func viewDidLayoutSubviews() {
        containView.center = view.center
        backgroundView.frame = view.frame
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

